package com.ym.manager.admin.redis;

import com.ym.utils.JsonUtil;
import com.ym.utils.ResultUtil;
import com.ym.utils.StringHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/redis")
public class RedsiInfoController {
    @Autowired
    private RedsiInfoService redsiInfoService;

    @RequestMapping("/message/index")
    public String messageIndex(){
        return "redis/msg_index";
    }
    @RequestMapping("/message/info")
    @ResponseBody
    public String mssageInfo(long sysId){
        Map<String,Object> result = ResultUtil.result();
        List<MessageResponseBean> unReadMessage = redsiInfoService.getUnReadMessage(sysId);
        if (!unReadMessage.isEmpty()) {
            unReadMessage = unReadMessage.stream().sorted((o1, o2) -> {
                if (StringHandler.isEmpty(o1.getCreateTime())) {
                    if (StringHandler.isEmpty(o2.getCreateTime())) {
                        return 0;
                    }
                    return 1;
                }
                if (StringHandler.isEmpty(o2.getCreateTime())) {
                    return -1;
                }
                return o1.getCreateTime().compareTo(o2.getCreateTime());
            }).collect(Collectors.toList());
            result.put("data","用户id【"+sysId+"】 消息推送条数:【"+Math.min(unReadMessage.size(), 200)+"】 推送内容:【"+ JsonUtil.toJson(unReadMessage.subList(0, Math.min(unReadMessage.size(), 200)))+"】");
        }
        return ResultUtil.toJSON(result);
    }
}
